'''
Created on 2011-3-8

@author: jeffrey
'''
from google.appengine.ext.db import djangoforms
from django.utils.translation import ugettext
from django import forms
from models import *
import service

class UserForm(djangoforms.ModelForm):
    name = forms.CharField(label=ugettext('User Name'),
                           error_messages={'required': ugettext('Please enter your name')},
                           required=True)
    gender = forms.ChoiceField(label=ugettext('Gender'), 
                               choices=((0, ugettext('Male')),(1, ugettext('Female'))),
                               required=False)
    logo = forms.URLField(label=ugettext('Logo'),
                          error_messages={'invalid': ugettext('Please input an invalid url')},
                          verify_exists=True,
                          required=False)
    email = forms.EmailField(label=ugettext('Email'),
                             error_messages={'invalid': ugettext('Please input an invalid email address')},
                             required=False)
    
    class Meta:
        model = User
        exclude = ['credit', 'lastlogtime', 'createtime']
    
    def clean_email(self):
        email = self.cleaned_data.get('email')
        if email:        
            if service.checkUserExistEmail(email, self.instance.id):
                raise forms.ValidationError(ugettext('User email already exists'))
        
        return email

class MessageForm(djangoforms.ModelForm):
    
    class Meta:
        model = Message
        exclude = ['createuser', 'isreceive', 'receiveuser', 'receivetime', 'createtime']